package com.cloud.service.wxsubscribe.impl;

import com.cloud.dao.wxsubscribe.WxSubscribeDao;
import com.cloud.model.entity.wxsubscribe.WxSubscribe;
import com.cloud.model.view.wxsubscribe.WxSubscribeView;
import com.cloud.service.wxsubscribe.WxSubscribeService;
import com.cloud.commons.utils.CloudCopyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* <b><code>WxSubscribeImpl</code></b>
* <p/>
* WxSubscribe的具体实现
* <p/>
* <b>Creation Time:</b> 2020-01-11 17:45:41.
*
* @author ${model.author}
* @version 1.0.0
* @since cloud-spring-boot-be 1.0.0
*/
@Service
public class WxSubscribeServiceImpl implements WxSubscribeService {

    /**
     * The Constant LOG.
     */
    private static final Logger LOG = LoggerFactory
            .getLogger(WxSubscribeServiceImpl.class);

    @Autowired
    private WxSubscribeDao wxSubscribeDao;

    @Override
    public WxSubscribeView getEntity(long id) {
        // 获取Entity
        WxSubscribe wxSubscribe = wxSubscribeDao.getOne(id);
        // 复制Dao层属性到view属性
        WxSubscribeView wxSubscribeView = new WxSubscribeView();
        CloudCopyUtils.copyBean(wxSubscribe, wxSubscribeView);
        return wxSubscribeView;
    }

    @Override
    public long getEntitiesCount() {
        return wxSubscribeDao.count();
    }

    @Override
    public List<WxSubscribeView> findAll() {
        List<WxSubscribeView> wxSubscribeViews = new ArrayList<>();
        List<WxSubscribe> wxSubscribes = wxSubscribeDao.findAll();
        for (WxSubscribe wxSubscribe : wxSubscribes){
            WxSubscribeView wxSubscribeView = new WxSubscribeView();
            CloudCopyUtils.copyBean(wxSubscribe, wxSubscribeView);
            wxSubscribeViews.add(wxSubscribeView);
        }
        return wxSubscribeViews;
    }

    @Override
    public WxSubscribeView saveEntity(WxSubscribeView wxSubscribeView)  {
        // 保存的业务逻辑
        WxSubscribe wxSubscribe = new WxSubscribe();
        CloudCopyUtils.copyBean(wxSubscribeView, wxSubscribe);
        // user数据库映射传给dao进行存储
        wxSubscribe.setCreateTime(System.currentTimeMillis());
        wxSubscribe.setUpdateTime(System.currentTimeMillis());
        wxSubscribe.setEnabled(1);
        wxSubscribeDao.save(wxSubscribe);
        CloudCopyUtils.copyBean(wxSubscribe,wxSubscribeView);
        return wxSubscribeView;
    }

    @Override
    public void deleteEntity(long id) {
        WxSubscribe wxSubscribe = new WxSubscribe();
        wxSubscribe.setId(id);
        wxSubscribeDao.delete(wxSubscribe);
    }

    @Override
    @Transactional(rollbackOn = { Exception.class })
    public void deleteEntities(String ids) {
        String[] entityIds= ids.split("\\,");
        List<WxSubscribe> wxSubscribes = new ArrayList<>();
        for(String entityId : entityIds){
            WxSubscribe wxSubscribe = new WxSubscribe();
            wxSubscribe.setId(Long.valueOf(entityId));
            wxSubscribes.add(wxSubscribe);
        }
        wxSubscribeDao.deleteInBatch(wxSubscribes);
    }

    @Override
    public void updateEntity(WxSubscribeView wxSubscribeView) {
        Specification<WxSubscribe> wxSubscribeSpecification = Optional.ofNullable(wxSubscribeView).map( s -> {
            return new Specification<WxSubscribe>() {
                @Override
                public Predicate toPredicate(Root<WxSubscribe> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicates = new ArrayList<>();
                    // id
                    predicates.add(criteriaBuilder.equal(root.get("id").as(Long.class), s.getId()));

                    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                    return criteriaQuery.getRestriction();
                }
            };
        }).orElseThrow(()->new IllegalArgumentException("WxSubscribeView is null"));


        // 获取原有的属性，把不变的属性覆盖
        Optional<WxSubscribe> wxSubscribeOptionalBySearch = wxSubscribeDao.findOne(wxSubscribeSpecification);
        wxSubscribeOptionalBySearch.map(wxSubscribeBySearch -> {
            CloudCopyUtils.copyBeanExcludeNull(wxSubscribeView,wxSubscribeBySearch);
            wxSubscribeBySearch.setUpdateTime(System.currentTimeMillis());
            wxSubscribeDao.save(wxSubscribeBySearch);
            return "";
        }).orElseThrow(()->new RuntimeException("无法找到id为" + wxSubscribeView.getId() + "的数据记录"));
    }
}
